package com.yuan.algorithms.training201607;

import java.util.Scanner;

/**
 * @author YouYuan
 * @contact 1265161633@qq.com
 * @date 2016年8月13日 上午8:44:24
 * @descript 
 */
public class NIM堆游戏 {

	static Scanner in = new Scanner(System.in);
	public static void main(String[] args) {
		while(in.hasNext()) {
			int n = in.nextInt();
			int[] nims = new int[n];
			for (int i = 0; i < n; i++) {
				nims[i] = in.nextInt();
			}
			byte[][] binaryMap = nimsToArray(nims);
			StringBuilder result = new StringBuilder();
			int index = -1;
			_A:for (int i = 0; i < binaryMap.length; i++) {
				result = new StringBuilder();
				index = i;
				boolean isFirst = true;//标记当前位置的数是否是第一次变大（更高位没有变小过）
				for (int j = 0; j < binaryMap[i].length; j++) {
					byte temp = 0;
					for (int k = 0; k < binaryMap.length; k++) {
						temp = (byte) (temp ^ binaryMap[k][j]);//计算当前列的异或结果
					}
					if (temp == 1) {
						if (binaryMap[i][j] == 0) {
							if (isFirst) {
								break;//不能拿这一堆，跳过
							} else {
								result.append(1);//原值是1，将当前值设为1，保持列的异或结果为0
							}
						} else {
							result.append(0);//原值是0，将当前值设为0，保持列的异或结果为0
							isFirst = false;//从1变为0，标记已经变小过
						}
					} else {
						result.append(binaryMap[i][j]);
					}
					if (j == binaryMap[i].length - 1) {//找到最后一位，说明当前结果可以作为解，跳出
						break _A;
					}
				}
			}
			int answer = Integer.parseInt(result.toString(), 2);
			if (answer == nims[0]) {
				System.out.println("此局输");
			} else {
				System.out.println("行"+(index+1)+"剩"+answer);
			}
		}
	}
	
	/**
	 * 将nim堆数据转换成二进制数组
	 * @param nims
	 * @return
	 */
	private static byte[][] nimsToArray(int[] nims) {
		int n = nims.length;
		String[] binarys = new String[n];
		int maxLength = Integer.MIN_VALUE;
		for (int i = 0; i < nims.length; i++) {
			binarys[i] = Integer.toBinaryString(nims[i]);
			if (binarys[i].length() > maxLength) {
				maxLength = binarys[i].length();
			}
		}
		byte[][] binaryMap = new byte[n][maxLength];
		for (int i = 0; i < binarys.length; i++) {
			String temp = binarys[i];
			for (int j = temp.length() - 1, k = maxLength - 1; j >= 0; j--, k--) {
				binaryMap[i][k] = (byte) Integer.parseInt(""+temp.charAt(j));
			}
		}
		return binaryMap;
	}

}
